package com.init.engineering.dao;


import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Branch;
import com.init.engineering.pojo.Taskplan;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface TaskplanDao {

    @Results(id = "TaskplanMap",value = {
            @Result(column = "bid",property = "bid"),
            @Result(column = "bid",property = "branch",javaType = Branch.class,one =
            @One(select = "com.init.engineering.dao.BranchDao.findBranchByBid",fetchType = FetchType.EAGER))
    })
    @Select("SELECT * from taskplan where status = 1")
    List<Taskplan> findTaskplans();


    @ResultMap("TaskplanMap")
    @Select({"<script> SELECT * FROM taskplan <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and tname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where> limit #{currentPage},#{pageSize}" +
            "</script>"})
    List<Taskplan> findTaskplansByPage(QueryPageBean queryPageBean);


    @Select({"<script> SELECT count(*) FROM taskplan <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and tname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where>" +
            "</script>"})
    long selectTotalCount(@Param("queryString")String queryString);



    @Update("UPDATE taskplan set status = 0 where tid = #{tid}")
    int updateTaskplanStatus(@Param("tid")String tid);


    @Update("<script>update taskplan <set><if test='tname != null'>tname = #{tname},</if>" +
            "<if test='bid != null'>bid = #{bid},</if>" +
            "<if test='starttime != null'>starttime = #{starttime},</if>" +
            "<if test='endtime != null'>endtime = #{endtime},</if>" +
            "<if test='updatetime != null'>updatetime = #{updatetime}</if>" +
            "</set> where tid = #{tid}</script>")
    int updateTaskplan(Taskplan taskplan);


    @Insert("insert into taskplan(tid,tname,bid,starttime,endtime,updatetime,status)" +
            " values(#{tid},#{tname},#{bid},#{starttime},#{endtime},#{updatetime},1)")
    int saveTaskplan(Taskplan taskplan);

    /**
     * 找到开发部门对应的所有任务计划
     * @param bid
     * @return
     */
    @Select("select * from taskplan where bid = #{bid} and status = 1")
    List<Taskplan> findTaskplanByBid(String bid);


    @Select("select * from taskplan where tid = #{tid} and status = 1")
    Taskplan findTaskplanByTid(String tid);

}
